package com.kitty.mina.filter;

import com.kitty.game.role.model.Role;
import com.kitty.game.server.message.RespOtherLogin;
import com.kitty.mina.HeartRecord;
import com.kitty.mina.Modules;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 心跳验证
 */
@Slf4j
public class HeartbeatFilter extends IoFilterAdapter {
    /**
     * 合法的心跳间隔应该10秒左右
     */
    private static final int REGULAR_TIME = 5000;

    /**
     * 存储心跳记录
     */
    private static Map<Long, HeartRecord> heartRecords = new ConcurrentHashMap<>();

    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session, Object packet) {
        Message message = (Message) packet;
        Role role = SessionUtils.getRoleBySession(session);
//        if (isHeartBeat(message.getModule(), role)) {
//            HeartRecord record = getRecord(role.getUid());
//            if (record == null) {
//                record = new HeartRecord();
//                record.setLastTime(System.currentTimeMillis());
//                record.setCurrTime(System.currentTimeMillis());
//                setRecord(role.getUid(), record);
//            } else {
//                record.setLastTime(record.getCurrTime());
//                record.setCurrTime(System.currentTimeMillis());
//                if (isIllegalHeartBeat(record)) {
//                    /**本次心跳非法*/
//                    log.error("[{}]心跳非法...非法次数[{}]", role.getName(), record.getIllegalCount() + 1);
//                    if (record.addCount()) {
//                        /**非法心跳达到上限*/
//                        RespOtherLogin respOtherLogin = new RespOtherLogin();
//                        respOtherLogin.setContent("检测到使用第三方辅助软件，请正常游戏");
//                        respOtherLogin.setResult((short) 2);
//                        MessagePusher.pushMessage(role, respOtherLogin);
//                        return;
//                    }
//                }
//            }
//        }
        nextFilter.messageReceived(session, message);
    }


    /**
     * 判断是否是角色登录后的心跳 登录前的心跳无需判断
     */
    private static boolean isHeartBeat(int moduleId, Role role) {
        return Modules.CMD_ECHO == moduleId && role != null;
    }

    /**
     * 取心跳记录
     */
    private static HeartRecord getRecord(long uid) {
        return heartRecords.get(uid);
    }

    /**
     * 存储心跳记录
     */
    private static void setRecord(long uid, HeartRecord record) {
        heartRecords.put(uid, record);
    }

    /**
     * 判断心跳是否合法
     * 心跳时间正常10秒一次 算上网络延迟应该不可能小于10秒
     * 所以这里判断心跳时间只要间隔小于10秒就算非法
     */
    private static boolean isIllegalHeartBeat(HeartRecord heartRecord) {
        return heartRecord.getCurrTime() - heartRecord.getLastTime() < REGULAR_TIME;
    }

    /**
     * 重置心跳记录
     */
    public static void cleanHeartBeatRecord(long uid) {
        heartRecords.remove(uid);
    }
}
